package com.zz.tijian.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.tijian.entity.Checkitem;
import com.zz.tijian.entity.Setmeal;
import com.zz.tijian.entity.Setmealdetailed;
import com.zz.tijian.entity.Users;
import com.zz.tijian.entity.req.CurrentUserInfo;
import com.zz.tijian.entity.res.Result;
import com.zz.tijian.entity.res.SetmealVo;
import com.zz.tijian.mapper.CheckitemMapper;
import com.zz.tijian.mapper.SetmealMapper;
import com.zz.tijian.mapper.SetmealdetailedMapper;
import com.zz.tijian.service.SetmealService;
import com.zz.tijian.service.SetmealdetailedService;
import com.zz.tijian.service.UsersService;
import com.zz.tijian.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 17958
* @description 针对表【setmeal】的数据库操作Service实现
* @createDate 2024-06-13 12:05:47
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealdetailedMapper setmealdetailedMapper;
    @Autowired
    private SetmealdetailedService setmealdetailedService;
    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private UsersService usersService;

    @Override
    public Result<List<SetmealVo>> getAll() {

        String userId = UserContext.currentUserId();
        Users users = usersService.getById(userId);
        Integer type=users.getSex();
        LambdaQueryWrapper<Setmeal>query=new LambdaQueryWrapper<>();
        query.eq(Setmeal::getType,type);
        // 获取所有套餐
        List<Setmeal> setmealList = this.list(query);

        // 转换为 SetmealVo 并查询每个套餐的检查项
        List<SetmealVo> setmealVoList = setmealList.stream().map(setmeal -> {
            SetmealVo setmealVo = new SetmealVo();
            setmealVo.setSmId(setmeal.getSmId());
            setmealVo.setName(setmeal.getName());
            setmealVo.setType(setmeal.getType());
            setmealVo.setPrice(setmeal.getPrice());

            // 查询套餐明细
            LambdaQueryWrapper<Setmealdetailed> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmealdetailed::getSmId, setmeal.getSmId());
            List<Setmealdetailed> details = setmealdetailedMapper.selectList(queryWrapper);
            List<Integer> checkItemIds = details.stream().map(Setmealdetailed::getCiId).collect(Collectors.toList());

            // 查询所有检查项
            List<Checkitem> checkItemList = checkitemMapper.selectBatchIds(checkItemIds);
            setmealVo.setCheckItemList(checkItemList);

            return setmealVo;
        }).collect(Collectors.toList());

        // 返回结果
        return Result.ok(setmealVoList);
    }

    /*
    获取单个套餐信息
     */
    @Override
    public SetmealVo getSetmealWithCheckItems(Integer smId) {
        SetmealVo setmealVo = new SetmealVo();
        setmealVo.setSmId(smId);

        // 查询套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(smId);
        setmealVo.setName(setmeal.getName());
        setmealVo.setType(setmeal.getType());
        setmealVo.setPrice(setmeal.getPrice());

        // 查询套餐明细
        LambdaQueryWrapper<Setmealdetailed> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmealdetailed::getSmId,smId);
        List<Setmealdetailed> details = setmealdetailedService.list(queryWrapper);
        List<Integer> checkItemIds = details.stream().map(Setmealdetailed::getCiId).collect(Collectors.toList());

        // 查询所有检查项
        List<Checkitem> checkItemList = checkitemMapper.selectBatchIds(checkItemIds);
        setmealVo.setCheckItemList(checkItemList);
        return setmealVo;
    }
}




